import 'package:FineArt/constants.dart';
import 'package:FineArt/core/api/module_api.dart';
import 'package:FineArt/core/api/resource_api.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/core/widget/grid/grid_item_photo.dart';
import 'package:FineArt/core/widget/grid/grid_staggered_item_photo.dart';
import 'package:FineArt/page/imageview/FavoriteImagePreview.dart';
import 'package:FineArt/page/imageview/ImagePreview.dart';
import 'package:FineArt/page/tab/component/CheckImage.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

class SearchResultPage extends StatefulWidget {
  final String path;
  final String title;
  SearchResultPage({this.path,this.title});

  static void showSearchResult(String path, String title, BuildContext context)
  {
    Navigator.of(context).push(MaterialPageRoute(builder: (context){
      return SearchResultPage(path: path,title: title);
    }));
  }

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _SearchResultState();
  }
}

class _SearchResultState extends State<SearchResultPage> {
  List<Resource> resources = [];
  int currentPage = 1;

  @override
  void initState() {
    // TODO: implement initState
    // TODO: implement build
    super.initState();
  }

  void _nextPage() {
    // LogUtil.d(StackTrace.current);
    ModuleApi().search(widget.path, "", currentPage, PAGE_SIZE).then((res) {
      if (!this.mounted) return;
      setState(() {
        if (res.data.list.isNotEmpty) {
          currentPage = res.data.pageIndex + 1;
          resources.addAll(res.data.list);
        }
      });
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _nextPage();
  }

  int tabIndex = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
        appBar: AppBar(
          elevation: 0,
          leading: IconButton(
            icon: Icon(Icons.arrow_back, color: Colors.black),
            onPressed: () => Navigator.of(context).pop(),
          ),
          brightness: Brightness.light,
          title: Text(
            widget.title,
            style: TextStyle(color: text_black, fontSize: setSp(18)),
          ),
          backgroundColor: Colors.white,
          automaticallyImplyLeading: false,
          centerTitle: true,
        ),
        body: Container(
          padding: EdgeInsets.symmetric(horizontal: 16),
          width: SizeConfig.screenWidth,
          child: Column(
            children: [
              SizedBox(
                height: getPortraitSHeight(10),
              ),
              Flexible(child: _widget_waterfall_grid()),
            ],
          ),
        ));
  }

  Widget _widget_waterfall_grid() {
    double width = MediaQuery.of(context).size.width * 0.99;
    ScrollController _scrollController = new ScrollController();
    MaterialHeader header =
        MaterialHeader(completeDuration: new Duration(milliseconds: 100));
    return EasyRefresh.custom(
      // controller: _controller,
      header: header,
      footer: MaterialFooter(),
      onRefresh: () async {
        currentPage = 1;
        resources.clear();
        _nextPage();
      },
      onLoad: () async {
        _nextPage();
      },
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: RefreshIndicator(
            onRefresh: () async {
              await Future.delayed(Duration(seconds: 5));
            },
            child: Padding(
              padding: const EdgeInsets.only(top: 6, left: 0, right: 0),
              child: StaggeredGridView.countBuilder(
                  shrinkWrap: true,
                  controller: _scrollController,
                  crossAxisCount: 6,
                  crossAxisSpacing: 2,
                  mainAxisSpacing: 2,
                  itemCount: resources.length,
                  itemBuilder: (context, index) {
                    var resource = resources[index];
                    return GridItemPhoto1(
                        url: resource.url,
                        width: width / 3,
                        // height: width / (3 * 0.73),
                        onTap: (i) {
                            ImagePreview.JumpToPreview(currentPage, widget.path, "", index, resources, context);
                        });
                  },
                  staggeredTileBuilder: (index) => StaggeredTile.fit(2)),
            ),
            // (index) => new StaggeredTile.count(2, index==0?2.5:3)),
          ),
        ),
      ],
    );
  }
}
